<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>



        <div id="divTest">

            [{ " checked " :" false" , " id " : " ", " productName " : " 微信支付", " productCode " : " VSP501", " feeRate
            " : " 0.001", " minimumCost " : " 0.001", " cappingCost " : " 0.001"},{ " checked " :" true" , " id " : "
            jldflkasjdf2", " productName " : " 支付宝支付", " productCode " : " VSP502", " feeRate " : " ", "
            minimumCost " : " 0.001", " cappingCost " : " 0.001"} ]
        </div>


        <div id="realData">
            [{"merchantId":"f62cf6e4beba4f58ab2cb42bd7c58bb4","defaultChannelId":"83a3d26f643c5d6e81a3ae1d48873ebc","flag":"1","id":"43e39141dc7e4257a2ff489388695d6a","productName":"微信支付","productCode":"XYF501","feeRate":"1.0000","minimumCost":"1.00","cappingCost":"1.00"},{"merchantId":"f62cf6e4beba4f58ab2cb42bd7c58bb4","defaultChannelId":"83a3d26f643c5d6e81a3ae1d48873ebc","flag":"0","id":"83979a5d13fc480ebfecd3c5f8e5ccef","productName":"微信支付","productCode":"XYF501","feeRate":"","minimumCost":"","cappingCost":""},{"merchantId":"f62cf6e4beba4f58ab2cb42bd7c58bb4","defaultChannelId":"83a3d26f643c5d6e81a3ae1d48873ebc","flag":"0","id":"","productName":"支付宝支付","productCode":"XYF511","feeRate":"","minimumCost":"","cappingCost":""},{"merchantId":"f62cf6e4beba4f58ab2cb42bd7c58bb4","defaultChannelId":"83a3d26f643c5d6e81a3ae1d48873ebc","flag":"1","id":"d93846577d174d56933c0af1edaad1b7","productName":"银联扫码支付-信用卡","productCode":"XYF551-CREDIT","feeRate":"","minimumCost":"1.00","cappingCost":"1.00"},{"merchantId":"f62cf6e4beba4f58ab2cb42bd7c58bb4","defaultChannelId":"83a3d26f643c5d6e81a3ae1d48873ebc","flag":"0","id":"","productName":"银联扫码支付-借记卡","productCode":"XYF551-DEBIT","feeRate":"","minimumCost":"","cappingCost":""}]


        </div>


        <script>
            /* js 校验数字正则 */
            var regOnlyNum = new RegExp("^[0-9]*$");
            /* js 去掉最后一个字符*/
            let str = "122889,";
            console.log("是否只是数字?" + regOnlyNum.test(str));
            str = str.slice(0, str.length - 1) ;
            console.log(str) ; //122889
            console.log("是否只是数字?" + regOnlyNum.test(str));

            /**
             * substring(from,to)
             * from 要截取的字符串的起始下标
             * to 截取到的字符串的下标 不包括这一项
             */

            str = "122889,";
            str = str.substring(0, str.length - 1)
            console.log(str);



            /* js 去掉all 空格 , Java提供了trim()方法, 但是js是没有的, so, 只能使用正则表达式去做 */
            str = document.getElementById("realData").textContent;

            str = str.replace(/\s*/g, "");
            console.log(str);




            function checkData(str) {
                  /* js json 转成js 对象*/
                  // alert(productList[0].toString());
                  //alert(productList[0]["checked"]);
                  var productList = eval('(' + str + ')');
                  console.log(productList[0]["flag"] == '1');
                  console.log(productList[1]["flag"] == "0");
                  var regOnlyNum = new RegExp("^[0-9]*$");
                for (var i = 0; i < productList.length; i++) {
                    console.log("i=" + i);
                    if (productList[i]["flag"] == '1'  && regOnlyNum.test( productList[i]["feeRate"] ) ) {
                        alert("参数错误");
                        console.info(i + ":" + productList[i]["feeRate"]);
                        return false;
                    }
                }
            }

            var right=checkData(str);

        </script>

    </body>
</html>
